<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

require './application/controllers/pc_controller.php';

class Permission extends PC_controller {

	public function __construct(){
		parent::__construct();
	}

	public function index(){
		$data['username'] = $this->m_app->getCurrentUserName();
		$data['permission'] = $this->m_app->outputPermission('permission');
		$data['menu'] = array(
			'mnu_system' => $this->m_app->outputSystemMenu4js(),
			'mnu_layout' => $this->m_app->outputLayoutMenu4js(),
		);
		$this->load->view('permission',$data);
	}

	public function getPermissionList(){
		$list = $this->m_permission->getPermissionList();
		$count = count($list);

		foreach($list as &$permission){
			$actionList = array();
			if(validate($permission['permission_action'])){
				$actionList = json_decode($permission['permission_action']);
			}
			$permission['action_count'] = count($actionList);
		}

		$this->response->setSuccess();
		$this->response->setItems($list);
		$this->response->setTotalCount($count);
		$this->response->responseExit();
	}

	public function createPermission(){
		$this->validate->setRule('permission_name','string');
		$this->validate->setRule('permission_name','message',$this->lang->line('error_group_name'));
		$this->validate->setRule('permission_category','string');
		$this->validate->setRule('permission_category','message',$this->lang->line('error_group_name'));
		if(!$this->validate->run($this->input->post())){
			
			$this->response->setDetail($this->validate->getMessage());
			$this->response->responseExit();
		}

		$info = $this->validate->getResult();
		$this->m_permission->createPermission($info);

		$this->response->setSuccess();
		$this->response->setDetail($this->lang->line('success_update'));
		$this->response->responseExit();
	}

	public function editPermission(){
		$this->validate->setRule('permission_id','integer');
		$this->validate->setRule('permission_id','message',$this->lang->line('error_group_name'));
		$this->validate->setRule('permission_name','string');
		$this->validate->setRule('permission_name','optional');
		$this->validate->setRule('permission_name','message',$this->lang->line('error_group_name'));
		$this->validate->setRule('permission_category','string');
		$this->validate->setRule('permission_name','optional');
		$this->validate->setRule('permission_category','message',$this->lang->line('error_group_name'));
		if(!$this->validate->run($this->input->post())){
			
			$this->response->setDetail($this->validate->getMessage());
			$this->response->responseExit();
		}

		$info = $this->validate->getResult();
		$this->m_permission->editPermission($info['permission_id'],$info);

		$this->response->setSuccess();
		$this->response->setDetail($this->lang->line('success_update'));
		$this->response->responseExit();
	}

	public function getActionList(){
		$this->validate->setRule('permission_id','integer');
		$this->validate->setRule('permission_id','message',$this->lang->line('error_group_name'));
		if(!$this->validate->run($this->input->post())){
			
			$this->response->setDetail($this->validate->getMessage());
			$this->response->responseExit();
		}

		$info = $this->validate->getResult();
		
		global $ctrllerArray;
		$list = array();
		$actionList = $this->m_permission->getPermissionAction($info['permission_id']);
		foreach($ctrllerArray as $ctrller){
			if(!class_exists($ctrller)){
				require(APPPATH.'controllers/'.$ctrller.EXT);
			}

			$class_methods = get_class_methods($ctrller);
	 
			foreach($class_methods as $method){
				if(!in_array($method,array('__construct','get_instance'))){
					$action = $ctrller.'.'.$method;
					$status = in_array($action,$actionList);
					$list[] = array(
						'controller' => $ctrller,
						'action' => $method,
						'status' => $status,
					);
				}
			}
		}
		$count = count($list);

		$this->response->setSuccess();
		$this->response->setItems($list);
		$this->response->setTotalCount($count);
		$this->response->responseExit();
	}

	public function editAction(){
		$this->validate->setRule('permission_id','integer');
		$this->validate->setRule('permission_id','message',$this->lang->line('error_parameter'));
		$this->validate->setRule('action','array');
		$this->validate->setRule('action','message',$this->lang->line('error_parameter'));
		if(!$this->validate->run($this->input->post())){
			
			$this->response->setDetail($this->validate->getMessage());
			$this->response->responseExit();
		}

		$info = $this->validate->getResult();
		$permission = array_values($info['action']);
		sort($permission);
		$info['permission_action'] = json_encode($permission);
		unset($info['action']);
		$this->m_permission->editPermission($info['permission_id'],$info);

		$this->response->setSuccess();
		$this->response->setDetail($this->lang->line('success_update'));
		$this->response->responseExit();
	}
}
/* End of file permission.php */
/* Location: ./application/controllers/permission.php */